package com.dao;

import com.pojo.Dept;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class DeptDao {
    //
//public void u(ResultSet rs){
//
//    dep.setDeptno(rs.getInt("deptno"));
//}
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入部门编号:");
        int a = input.nextInt();
        System.out.println("请输入部门名称:");
        String b = input.next();
        System.out.println("请输入部门地址:");
        String c = input.next();
        insert(a,b, c);
    }

    public List<Dept> selectByDname(String dname){
        List<Dept> depts = new ArrayList<>();

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        Connection conn = null;
        PreparedStatement psmt = null;
        ResultSet rs = null;//接收查询返回的数据的结果集
        //创建连接对象
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=UTF-8",
                    "root","123456");
            //sql
            String sql = "select deptno,dname,loc from dept " +
                    "where dname like ?";
            psmt = conn.prepareStatement(sql);
            psmt.setObject(1,"%"+dname+"%");
            //
            rs = psmt.executeQuery();
            //.next读取下一行数据
            while (rs.next()){
                int deptno = rs.getInt("deptno");
                String dn = rs.getString("dname");
                String l = rs.getString("loc");
                Dept dept = new Dept(deptno,dn,l);
                depts.add(dept);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                rs.close();
                psmt.close();
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }

        return depts;

    }
    public static void insert(int deptno, String dname, String loc){
        //1.加载驱动类
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            //创建数据库连接对象

                connection =   DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=UTF-8",
                    "root","123456");

            //预处理命令

            String sql = "insert into dept(deptno,dname,loc) values(?,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            //为?赋值
            preparedStatement.setObject(1,deptno);
            preparedStatement.setObject(2,dname);
            preparedStatement.setObject(3,loc);
            //执行sql语句,接收返回值
            int result = preparedStatement.executeUpdate();
            System.out.println("数据库受影响的行数:"+result);
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            try {
                preparedStatement.close();
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }

    }
}
